package com.partsoft.dits.integrate;

import java.sql.Types;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import com.partsoft.dits.AbstractDataEdgeDescriptor;
import com.partsoft.dits.DataEdgeDescriptor;
import com.partsoft.dits.LdapDataEdgeDescriptor;
import com.partsoft.dits.message.DataProperty;

public class LdapEntityEdgeDescriptor extends AbstractDataEdgeDescriptor implements DataEdgeDescriptor,
		LdapDataEdgeDescriptor, Cloneable, BeanNameAware, InitializingBean {

	private String id;

	private String transformScript;

	private String ldapContextId;

	private String entityParentDN;

	private String entityPropertyId;
	
	private String entityClassName;

	private List<DataProperty> entityProperties = null;

	private String transformScriptPath;
	
	public LdapEntityEdgeDescriptor(){}
	
	public LdapEntityEdgeDescriptor(String ldapContextId, String entityParentDN, String entityPropertyId,
			String entityClassName) {
		this.ldapContextId = ldapContextId;
		this.entityParentDN = entityParentDN;
		this.entityPropertyId = entityPropertyId;
		this.entityClassName = entityClassName;
	}



	public String getTransformScript() {
		return transformScript;
	}

	public void setTransformScript(String transformScript) {
		this.transformScript = transformScript;
	}

	@Override
	public LdapEntityEdgeDescriptor clone() {
		LdapEntityEdgeDescriptor clone = (LdapEntityEdgeDescriptor) super.clone();
		return clone;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setBeanName(String s) {
		setId(s);
	}

	public String getTransformScriptPath() {
		return transformScriptPath;
	}

	public void setTransformScriptPath(String transformScriptPath) {
		this.transformScriptPath = transformScriptPath;
	}

	public String getLdapContextId() {
		return ldapContextId;
	}

	public void setLdapContextId(String ldapContextId) {
		this.ldapContextId = ldapContextId;
	}

	public String getEntityParentDN() {
		return entityParentDN;
	}

	public void setEntityParentDN(String entityParentDN) {
		this.entityParentDN = entityParentDN;
	}

	public String getEntityPropertyId() {
		return entityPropertyId;
	}

	public void setEntityPropertyId(String entityPropertyId) {
		this.entityPropertyId = entityPropertyId;
	}

	@SuppressWarnings("unchecked")
	public List<DataProperty> getEntityProperties() {
		return entityProperties == null ? Collections.EMPTY_LIST : Collections.unmodifiableList(entityProperties);
	}

	public void setEntityProperties(List<DataProperty> entityProperties) {
		this.entityProperties = entityProperties;
	}
	
	public Set<String> getEntityPropertiesNameSet() {
		List<DataProperty> list = getEntityProperties();
		Set<String> result = new HashSet<String>(list.size());
		for (DataProperty dp : list) {
			result.add(dp.getPropertyName());
		}
		return result;
	}

	public String getEntityPropertyNames() {
		StringBuffer buffer = new StringBuffer();
		boolean append = false;
		for (DataProperty cln : getEntityProperties()) {
			if (append)
				buffer.append(";");
			append = true;
			buffer.append(cln.getPropertyName()).append("=").append(cln.getValueType());
		}
		return buffer.toString();
	}

	public void addEntityProperty(DataProperty entityProperty) {
		Assert.notNull(entityProperty);
		if (entityProperties == null) {
			entityProperties = new LinkedList<DataProperty>();
			entityProperties.add(entityProperty);
		} else if (!entityProperties.contains(entityProperty)) {
			entityProperties.add(entityProperty);
		}
	}

	public void setEntityPropertyNames(String propertyNames) {
		Assert.hasText(propertyNames, "必须设置实体属性");
		String[] keys = propertyNames.split(";");
		Properties nameProps = StringUtils.splitArrayElementsIntoProperties(keys, "=");
		for (Object oc : nameProps.keySet()) {
			String clnName = oc.toString();
			int keyType = Types.VARCHAR;
			if (StringUtils.hasText(nameProps.getProperty(clnName)))
				try {
					keyType = Integer.parseInt(nameProps.getProperty(clnName));
				} catch (NumberFormatException e) {
				}
			addEntityProperty(new DataProperty(clnName, keyType));
		}
	}
	
	public String getEntityClassName() {
		return entityClassName;
	}

	public void setEntityClassName(String entityClassName) {
		this.entityClassName = entityClassName;
	}

	public void afterPropertiesSet() throws Exception {

	}
	
	@Override
	public String toString() {
		return new StringBuilder("LDAP").append(super.toString()).toString();
	}

}